
from sanic import Request

from app.utils.JwtUtils import JwtUtils


class RequestUtils:
    request:Request=None

    def __init__(self) -> None:
        self.request=Request.get_current()

    def getCurrentUserId(self):
        payload=JwtUtils.decodeToken(self.getToken())
        return int(payload['userId'])
    
    def getCurrentProjectId(self):
        projectId=self.request.headers['x-project-id']
        if projectId is not None:
            return int(projectId)
        return -1
    
    def getToken(self):
        return self.request.headers['x-token']
    
    def getPageData(self):
        page=self.request.get_args().get('page')
        if page is None:
            page= 1
        
        page_count=self.request.get_args().get('page_count')
        if page_count is None:
            page_count= 10
        return {"page":page,"page_count":page_count,"offset":(int(page)-1)*int(page_count)}